﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Beetle.Web.Composite;
using System.Web.UI;
using System.ComponentModel;
using Beetle.Web.Extensions;
using System.Web.UI.WebControls;
using System.Globalization;

namespace Beetle.Web.UI.WebControls
{
    public class RadioButtonList : System.Web.UI.WebControls.RadioButtonList, IExtensibleObject<Beetle.Web.UI.WebControls.RadioButtonList>, IEditStatus
    {
        private int _offset = 0;
        private RadioButton _controlToRepeat = null;

        private RadioButton ControlToRepeat
        {
            get
            {
                if (this._controlToRepeat == null)
                {
                    this._controlToRepeat = new RadioButton();
                    this._controlToRepeat.EnableViewState = false;
                    this.Controls.Add(this._controlToRepeat);
                    this._controlToRepeat.AutoPostBack = this.AutoPostBack;
                    this._controlToRepeat.CausesValidation = this.CausesValidation;
                    this._controlToRepeat.ValidationGroup = this.ValidationGroup;
                }
                return this._controlToRepeat;
            }
        }

        #region IEditStatus 成员

        [DefaultValue(true)]
        public bool IsEditMode
        {
            get
            {
                object o = ViewState["IsEditMode"];
                return o != null ? (bool)o : true;
            }

            set
            {
                ViewState["IsEditMode"] = value;
            }
        }

        private bool? _InternalEditMode = null;
        public bool? InternalEditMode
        {
            get
            {
                return this._InternalEditMode;
            }
        }

        public void SetInternalEditModeValue(bool? b)
        {
            this._InternalEditMode = b;
        }

        public bool CheckEditMode()
        {
            if (!this.IsEditMode)
            {
                return false;
            }

            return this.GetEditMode();
        }


        #endregion


        #region IExtensibleObject<DropDownList> 成员
        private ExtensionCollection<Beetle.Web.UI.WebControls.RadioButtonList> extensions = null;
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ExtensionCollection<Beetle.Web.UI.WebControls.RadioButtonList> Extensions
        {
            get
            {
                return extensions;
            }
        }
        #endregion

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public override System.Web.UI.WebControls.ListItemCollection Items
        {
            get
            {
                return base.Items;
            }
        }

        public RadioButtonList()
        {
            this.extensions = new ExtensionCollection<RadioButtonList>(this);
        }

        protected override void RenderItem(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex, System.Web.UI.WebControls.RepeatInfo repeatInfo, HtmlTextWriter writer)
        {
            bool isEditMode = this.GetEditMode();

            if (isEditMode)
            {
                base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
            }
            else
            {
                RadioButton controlToRepeat = this.ControlToRepeat;
                int num = repeatIndex + this._offset;
                ListItem item = this.Items[num];
                controlToRepeat.Attributes.Clear();

                if (item.Attributes.Count > 0)
                {
                    foreach (string str in item.Attributes.Keys)
                    {
                        controlToRepeat.Attributes[str] = item.Attributes[str];
                    }
                }

                controlToRepeat.ID = num.ToString(NumberFormatInfo.InvariantInfo);
                controlToRepeat.Text = item.Text;
                controlToRepeat.Attributes["value"] = item.Value;
                controlToRepeat.Checked = item.Selected;
                controlToRepeat.Enabled = false;
                controlToRepeat.TextAlign = this.TextAlign;
                controlToRepeat.RenderControl(writer);
            }
        }
    }
}
